package com.happy_hao.msg.util;

import com.happy_hao.msg.dto.EmailRequest;
import com.happy_hao.msg.dto.PeriodicSendRequest;
import com.happy_hao.msg.mapper.EmailMapper;
import com.happy_hao.msg.po.Email;
import com.happy_hao.msg.po.Sender;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

@Component
public class EmailUtil {

    @Resource
    private EmailMapper emailMapper;

    @Resource
    private SenderUtil senderUtil;


    public void send(EmailRequest emailRequest) throws MessagingException, UnsupportedEncodingException {
        Sender sender = senderUtil.getSender(emailRequest.getUsername());
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding("utf-8");
        javaMailSender.setHost(sender.getHost());
        javaMailSender.setPort(465);
        javaMailSender.setUsername(sender.getUsername());
        javaMailSender.setPassword(sender.getPassword());
        javaMailSender.setProtocol("smtps");
        Properties properties = new Properties();
        properties.put("mail.smtp.connectiontimeout", 5000);
        properties.put("mail.smtp.timeout", 3000);
        properties.put("mail.smtp.writetimeout", "5000");
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.starttls.enable", true);
        properties.put("mail.smtp.starttls.required", true);
        javaMailSender.setJavaMailProperties(properties);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, false);
        helper.setFrom(emailRequest.getUsername(),emailRequest.getFromName());
        if(emailRequest.getToMailbox().contains(",")){
            String[] toMailboxes = emailRequest.getToMailbox().split(",");
            helper.setTo(toMailboxes);
        }else {
            helper.setTo(emailRequest.getToMailbox());
        }
        helper.setSubject(emailRequest.getSubject());
        helper.setText(emailRequest.getContent(),true);
        javaMailSender.send(message);
    }

    public void send(Email email) throws MessagingException, UnsupportedEncodingException {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding("utf-8");
        javaMailSender.setHost(email.getHost());
        javaMailSender.setPort(465);
        javaMailSender.setUsername(email.getUsername());
        javaMailSender.setPassword(email.getPassword());
        javaMailSender.setProtocol("smtps");
        Properties properties = new Properties();
        properties.put("mail.smtp.connectiontimeout", 5000);
        properties.put("mail.smtp.timeout", 3000);
        properties.put("mail.smtp.writetimeout", "5000");
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.starttls.enable", true);
        properties.put("mail.smtp.starttls.required", true);
        javaMailSender.setJavaMailProperties(properties);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, false);
        helper.setFrom(email.getUsername(),email.getFromName());
        if(email.getToMailbox().contains(",")){
            String[] toMailboxes = email.getToMailbox().split(",");
            helper.setTo(toMailboxes);
        }else {
            helper.setTo(email.getToMailbox());
        }
        helper.setSubject(email.getSubject());
        helper.setText(email.getContent(),true);
        javaMailSender.send(message);
    }

    public void newScheduledSend(EmailRequest emailRequest) {
        Email email = new Email();
        Sender sender = senderUtil.getSender(emailRequest.getUsername());
        email.setEmailId(SnowFlakeUtil.getSnowFlakeId());
        email.setHost(sender.getHost());
        email.setPort(465);
        email.setUsername(sender.getUsername());
        email.setPassword(sender.getPassword());
        email.setFromMailbox(sender.getUsername());
        email.setFromName(emailRequest.getFromName());
        email.setToMailbox(emailRequest.getToMailbox());
        email.setSubject(emailRequest.getSubject());
        email.setContent(emailRequest.getContent());
        email.setSendTime(emailRequest.getEndTime());
        email.setCreateAt(new Date());
        emailMapper.insert(email);
    }


    public  void periodicSend(PeriodicSendRequest periodicSendRequest) {
        Sender sender = senderUtil.getSender(periodicSendRequest.getFromEmail());
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setDefaultEncoding("utf-8");
        javaMailSender.setHost(sender.getHost());
        javaMailSender.setPort(465);
        javaMailSender.setUsername(sender.getUsername());
        javaMailSender.setPassword(sender.getPassword());
        javaMailSender.setProtocol("smtps");
        Properties properties = new Properties();
        properties.put("mail.smtp.connectiontimeout", 5000);
        properties.put("mail.smtp.timeout", 3000);
        properties.put("mail.smtp.writetimeout", "5000");
        properties.put("mail.smtp.auth", true);
        properties.put("mail.smtp.starttls.enable", true);
        properties.put("mail.smtp.starttls.required", true);
        javaMailSender.setJavaMailProperties(properties);

        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();//创建一个邮件消息
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
            message.setFrom(periodicSendRequest.getFromEmail(),"msg项目");
            message.setTo(periodicSendRequest.getToEmail());
            message.setSubject(periodicSendRequest.getSubject());
            message.setText(periodicSendRequest.getContent(),true);
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

//    @Value(value = "${spring.mail.username}")
//    private String from;
//    @Resource
//    private JavaMailSender javaMailSender;

    public Boolean sendSampleMail(String to, String subject, String context) {
//        try {
//            MimeMessage mimeMessage = javaMailSender.createMimeMessage();//创建一个邮件消息
//            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
////            message.setFrom(from,"系统默认消息");
//            message.setTo(to);
//            message.setSubject(subject);
//            message.setText(context,true);
//            javaMailSender.send(mimeMessage);
//        } catch (Exception e) {
//            return false;
//        }
        return true;
    }
}
